package com.gewu.bookeeping.View.ItemFragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gewu.bookeeping.MyDb.bean.MesBeanIn;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.ALiDaoOutPerson;
import com.gewu.bookeeping.MyDb.dao.MesInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoOutPerson;
import com.gewu.bookeeping.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ForOneItem1Fargemnt extends Fragment {


    private PieChart pieChart;
    private TextView tv;
    private int index;
    private int type;
    private List<?> quer;
    private List<PieEntry> strings = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_for_one_item1_fargemnt, container, false);
        initview(inflate);
        setDaet();
        return inflate;
    }

    private void initview(View inflate) {
        pieChart = inflate.findViewById(R.id.pieChart);
        tv = inflate.findViewById(R.id.tv);
        switch (type) {
            case 0:
                if (index == 0) {
                    WeChtDaoOutPerson weChtDaoOutPerson = new WeChtDaoOutPerson(getContext());
                    quer = weChtDaoOutPerson.quer();
                } else {
                    WeChtDaoInPerson weChtDaoInPerson = new WeChtDaoInPerson(getContext());
                    quer = weChtDaoInPerson.quer();
                }
                break;
            case 1:
                if (index == 0) {
                    ALiDaoOutPerson aLiDaoOutPerson = new ALiDaoOutPerson(getContext());
                    quer = aLiDaoOutPerson.quer();
                } else {
                    ALiDaoInPerson aLiDaoInPerson = new ALiDaoInPerson(getContext());
                    quer = aLiDaoInPerson.quer();
                }
                break;
        }
    }

    /**
     * 类型 收入还是支出 微信/支付宝/短信
     *
     * @param index
     * @param flage
     */
    public void add(int index, int flage) {
        this.index = index;
        this.type = flage;
    }

    private void setDaet() {
        float v = new Random().nextInt(100);
        strings.add(new PieEntry(2, "单个支出/收入"));
        strings.add(new PieEntry(v, "用户总支出/收入"));

        PieDataSet dataSet = new PieDataSet(strings, "单个支出占比总支出的图");

        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.selectitem));
        dataSet.setColors(colors);

        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);

        pieChart.setData(pieData);
        pieChart.invalidate();

        if (v > 5 && v < 40) {
            tv.setText("您的消费记录良好,继续保持");
        } else if (v > 40 && v < 80) {
            tv.setText("您的消费记录真的是太棒啦，继续保持努力争做月光族");
        } else if (v > 80) {
            tv.setText("您的消费记录良好,继续保持，继续保持再过一个月你就可以秦家荡产啦");
        } else if (v == 0) {
            tv.setText("您的消费记录良好,您没有消费记录");
        } else {
            tv.setText("您的消费记录良好,继续保持");
        }
    }

}